using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using OVS.BusinessObjects;
using System.Windows;

namespace OVS.DAL
{
	public partial class AlertDAO
	{

        public Alert AlertOfCameraAndDetectedObject(int came_id, int imha_id, int maxLastAlertInMiliseconde)
        {
            try
            {
                Alert oAlert = new Alert();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand(
                    "SELECT ALert.* " +
                    "FROM [Alert] INNER JOIN Image_Details ON Alert.imde_id = Image_Details.imde_id "+
                    "WHERE (Alert.[came_id]=@came_id AND Image_Details.[imha_id]=@imha_id AND "+
                    "DATEDIFF(MILLISECOND, Alert.aler_dt, GETDATE()) <= @maxLastAlertInMiliseconde AND "
                    + "Alert.[vide_id] IS NULL)"                    
                    , CommandType.Text);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@came_id", DbType.Int32, came_id));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@imha_id", DbType.Int32, imha_id));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@maxLastAlertInMiliseconde", DbType.Int32, maxLastAlertInMiliseconde));

                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    oAlert.aler_id = Convert.ToInt32(oDbDataReader["aler_id"]);

                    if (oDbDataReader["came_id"] != DBNull.Value)
                        oAlert.came_id = Convert.ToInt32(oDbDataReader["came_id"]);

                    if (oDbDataReader["aler_dt"] != DBNull.Value)
                        oAlert.aler_dt = Convert.ToDateTime(oDbDataReader["aler_dt"]);

                    if (oDbDataReader["aler_lvl"] != DBNull.Value)
                        oAlert.aler_lvl = Convert.ToInt32(oDbDataReader["aler_lvl"]);

                    if (oDbDataReader["aler_status"] != DBNull.Value)
                        oAlert.aler_status = Convert.ToBoolean(oDbDataReader["aler_status"]);

                    if (oDbDataReader["aler_image_traj"] != DBNull.Value)
                        oAlert.aler_image_traj = Convert.ToString(oDbDataReader["aler_image_traj"]);

                    if (oDbDataReader["vide_id"] != DBNull.Value)
                        oAlert.vide_id = Convert.ToInt32(oDbDataReader["vide_id"]);

                    if (oDbDataReader["aler_image"] != DBNull.Value)
                        oAlert.aler_image = Convert.ToString(oDbDataReader["aler_image"]);

                    if (oDbDataReader["imde_id"] != DBNull.Value)
                        oAlert.imde_id = Convert.ToInt32(oDbDataReader["imde_id"]);
                }
                oDbDataReader.Close();
                return oAlert;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<Alert> GetAlertsDateDesc()
        {
            try
            {
                List<Alert> lstAlerts = new List<Alert>();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [aler_id],[came_id],[aler_dt],[aler_lvl],[aler_status],[aler_image_traj],[vide_id],[aler_image],[imde_id],[user_id] FROM [Alert] order by aler_dt desc", CommandType.Text);
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    Alert oAlert = new Alert();
                    oAlert.aler_id = Convert.ToInt32(oDbDataReader["aler_id"]);

                    if (oDbDataReader["came_id"] != DBNull.Value)
                        oAlert.came_id = Convert.ToInt32(oDbDataReader["came_id"]);

                    if (oDbDataReader["aler_dt"] != DBNull.Value)
                        oAlert.aler_dt = Convert.ToDateTime(oDbDataReader["aler_dt"]);

                    if (oDbDataReader["aler_lvl"] != DBNull.Value)
                        oAlert.aler_lvl = Convert.ToInt32(oDbDataReader["aler_lvl"]);

                    if (oDbDataReader["aler_status"] != DBNull.Value)
                        oAlert.aler_status = Convert.ToBoolean(oDbDataReader["aler_status"]);

                    if (oDbDataReader["aler_image_traj"] != DBNull.Value)
                        oAlert.aler_image_traj = Convert.ToString(oDbDataReader["aler_image_traj"]);

                    if (oDbDataReader["vide_id"] != DBNull.Value)
                        oAlert.vide_id = Convert.ToInt32(oDbDataReader["vide_id"]);

                    if (oDbDataReader["aler_image"] != DBNull.Value)
                        oAlert.aler_image = Convert.ToString(oDbDataReader["aler_image"]);

                    if (oDbDataReader["imde_id"] != DBNull.Value)
                        oAlert.imde_id = Convert.ToInt32(oDbDataReader["imde_id"]);

                    if (oDbDataReader["user_id"] != DBNull.Value)
                        oAlert.user_id = Convert.ToInt32(oDbDataReader["user_id"]);
                    lstAlerts.Add(oAlert);
                }
                oDbDataReader.Close();
                return lstAlerts;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
